/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.ns('refrigeratory');

refrigeratory.pn_config = Ext.extend(Ext.Panel, {
	
    url: refrigeratory.REQUEST_URL

    ,initComponent:function() {
 
        // hard coded config (it cannot be changed while instantiating)
        // {{{
        var config = {        	
        	title: this.titleText
        	,id : 'pn_config'
			,closable: true
    		,autoScroll: true
    		,style:"height:100%;"
    		,bodyStyle:"height:100%;"
        }; // eo config object	       
        // apply config
        Ext.apply(this, Ext.apply(this.initialConfig, config));
        // }}}
        // call parent
        refrigeratory.pn_config.superclass.initComponent.apply(this, arguments);
        // parent call post-processing, e.g. install event handlers
 
    } // eo function initComponent

    ,onRender:function() {
     
        // parent call pre-processing
 
        // call parent
        refrigeratory.pn_config.superclass.onRender.apply(this, arguments);
 
        // parent call post-processing, e.g. install event handlers on rendered components
        this.add(this.createPanel());
        this.doLayout();
 
    } // eo function onRender
    
    // 需本地化的文本
    ,titleText: '系统参数设置'
	
	,createPanel: function(){ 
		var configGrid =this.configGrid= new Ext.grid.PropertyGrid({
			title: '系统参数配置',
			id: 'configGrid',
			autoHeight: true,
			width: 300,
			tbar:this.createTbar(),
			source: {} //initialize source config object
		});
		
		var propertyStore = new Ext.data.JsonStore({
			autoLoad: true,  //autoload the data
			baseParams: {action:'config',operation: 'get'},
			url: 'get.php',
			root: 'data',
			fields: ['name', 'value', 'description'],
			listeners: {
				load: {
					fn: function(store, records, options){
					
						PPGsource = {};
						for(var i = 0; i < records.length; i++){
						  PPGsource[records[i].get('name')]=records[i].get('value');
						}		
						// get the property grid component
						var propGrid = Ext.getCmp('configGrid');
						// make sure the property grid exists
						if (propGrid) {
							// populate the property grid with store data
							propGrid.setSource(PPGsource);
						}
					}
				}
			}
		}); 
		return configGrid;
	}
	
	,createTbar:function(){
		var tb = [];
    	this.btnRefresh = new Ext.Toolbar.Button({
    		text: "保存"
    		,handler: this.onBtnSaveClick.createDelegate(this)
    	});
    	tb.push(this.btnRefresh);
    	return tb;
	}
	
	,onBtnSaveClick:function(){
		var store = this.configGrid.getStore();		
		var recordData=[]; 
		Ext.each(store.getRange(),
					function(record){ 
						recordData.push(record.data); 
				}); 
		var jsonData=Ext.encode(recordData); 
		this.getEl().mask("正在保存...");
    	Ext.Ajax.request({
    		url: this.url
    		,params:{
    			action:'config'
    			,operation: 'update'
    			,jsonData: jsonData
    		}
    		,success: function(response){
				var r = Ext.decode(response.responseText);				
				if (r.success == true){
					Ext.MessageBox.alert("提示", r.info);
				}
				else{
					Ext.MessageBox.alert("提示", r.info);
				}
			}
    		,scope: this
    	});
		this.getEl().unmask();
	}
	
}); // eo extend
// register xtype
Ext.reg('pn_config', refrigeratory.pn_config); 